package com.evan.review.viewpager;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.evan.review.R;

public class VideoFragment extends Fragment {
    
    private static final String TAG = "VideoFragment";
    private static final String ARG_VIDEO_NUMBER = "video_number"; // 视频编号参数
    private static final String ARG_BACKGROUND_COLOR = "background_color"; // 背景颜色参数
    
    private int videoNumber; // 视频编号
    private int backgroundColor; // 背景颜色
    
    private ImageView videoThumbnail; // 视频缩略图
    private TextView tvVideoNumber; // 视频编号文本
    private TextView tvUsername; // 用户名文本
    private TextView tvDescription; // 视频描述文本
    private TextView tvLikesCount; // 点赞数文本
    
    /**
     * 创建VideoFragment实例的工厂方法
     * @param videoNumber 视频编号
     * @param backgroundColor 背景颜色
     * @return VideoFragment实例
     */
    public static VideoFragment newInstance(int videoNumber, int backgroundColor) {
        VideoFragment fragment = new VideoFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_VIDEO_NUMBER, videoNumber);
        args.putInt(ARG_BACKGROUND_COLOR, backgroundColor);
        fragment.setArguments(args);
        Log.d(TAG, "newInstance: videoNumber=" + videoNumber);
        return fragment;
    }
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            // 从参数中获取视频编号和背景颜色
            videoNumber = getArguments().getInt(ARG_VIDEO_NUMBER);
            backgroundColor = getArguments().getInt(ARG_BACKGROUND_COLOR);
        }
        Log.d(TAG, "onCreate: videoNumber=" + videoNumber);
    }
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: videoNumber=" + videoNumber);
        View view = inflater.inflate(R.layout.fragment_video, container, false);
        
        // 初始化视图
        videoThumbnail = view.findViewById(R.id.video_thumbnail);
        tvVideoNumber = view.findViewById(R.id.tv_video_number);
        tvUsername = view.findViewById(R.id.tv_username);
        tvDescription = view.findViewById(R.id.tv_description);
        tvLikesCount = view.findViewById(R.id.tv_likes_count);
        
        // 设置背景颜色
        view.setBackgroundColor(backgroundColor);
        
        // 设置基本信息
        tvVideoNumber.setText("视频 " + videoNumber);
        tvUsername.setText("@用户" + videoNumber);
        tvDescription.setText("这是视频" + videoNumber + "的示例描述。 #示例标签 #视频");
        tvLikesCount.setText(String.valueOf(videoNumber * 100));
        
        return view;
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG, "onViewCreated: videoNumber=" + videoNumber);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        // 在实际应用中，此处应开始播放视频
        Log.d(TAG, "onResume: videoNumber=" + videoNumber + " - 在实际应用中，此处应开始播放视频");
    }
    
    @Override
    public void onPause() {
        super.onPause();
        // 在实际应用中，此处应暂停视频播放
        Log.d(TAG, "onPause: videoNumber=" + videoNumber + " - 在实际应用中，此处应暂停视频播放");
    }
    
    /**
     * 用于ViewPager1的可见性回调方法
     * 注意：此方法在ViewPager2中已废弃
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d(TAG, "setUserVisibleHint: videoNumber=" + videoNumber + ", isVisibleToUser=" + isVisibleToUser);
        
        if (isVisibleToUser) {
            // 可见时应开始播放视频
        } else {
            // 不可见时应暂停视频播放
        }
    }
} 